%%%%%%%%%%%%%%%%%%%%%%%%%%% Chapter 11 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Ticket distribution and distributed objects

% Authors: Peter Van Roy and Seif Haridi
% May 9, 2003

% This is the Distribution module, which provides primitives for
% offering and taking a ticket by means of URLs and primitives for
% creating various kinds of distributed objects.

functor
import Connection Pickle
export
   offer:Offer take:Take
   newStat:NewStat newActive:NewActive
   newAsync:NewAsync newAsyncExc:NewAsyncExc
define
   proc {Offer X FN}
      {Pickle.save {Connection.offerUnlimited X} FN}
   end
      
   fun {Take FN}
      {Connection.take {Pickle.load FN}}
   end

   fun {NewStat Class Init}
      {MakeStat {New Class Init}}
   end
      
   fun {MakeStat PO}
      S P={NewPort S}
      N={NewName}
   in 
      thread 
         for M#R in S do
            thread 
               try {PO M} R=N catch X then R=X end 
            end 
         end
      end 
      proc {$ M}
         R in 
         {Send P M#R}
         if R==N then skip else raise R end end 
      end 
   end
      
   fun {NewActive Class Init}
      S P={NewPort S}
      Obj={New Class Init}
   in
      thread
         for M in S do
            try {Obj M} catch _ then skip end
         end
      end
      proc {$ M} {Send P M} end
   end
      
   NewAsync=NewActive
      
   fun {NewAsyncExc Class Init}
      S P={NewPort S}
      Obj={New Class Init}
   in
      thread
         for M#X in S do
            try {Obj M} X=unit catch Y then X=Y end
         end
      end
      proc {$ M X} {Send P M#X} end
   end
end

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
